package com.hp.test.java.io.file;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO 注释
 *
 * @author hupan
 * @date 2020-03-06 18:02
 */
public class Rename {

    static List<File> files = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        File root = new File("d:\\user\\01412764\\desktop\\");
        // File root = new File("C:\\Users\\Administrator\\桌面\\视频");
        List<File> files = scanDirRecursion(root);

        System.out.println(files);

        // renameAudio(files);
        // renameVideo(files);
//        getVideoName(files);
    }

    public static void renameAudio(List<File> files) {
        for (File file : files) {
            if (file.getName().contains("audio.m4s")) {
                File dest = new File(file.getParentFile(), file.getName().replace("m4s", "wav"));
                file.renameTo(dest);
            }
        }
    }

    public static void renameVideo(List<File> files) {
        for (File file : files) {
            if (file.getName().endsWith("video.m4s")) {
                File dest = new File(file.getParentFile(), file.getName().replace("m4s", "mp4"));
                file.renameTo(dest);
            }
        }
    }

    public static void getVideoName(List<File> files) throws IOException {
        for (File file : files) {
            if (file.getName().equals("entry.json")) {
                String contrent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
                JSONObject jsonObject = JSON.parseObject(contrent);
                JSONObject pageData = jsonObject.getJSONObject("page_data");
                System.out.println(pageData.getString("part"));
            }
        }
    }

    // 递归实现遍历文件夹
    public static List<File> scanDirRecursion(File file) {

        if (file != null) {
            if (file.isDirectory()) {
                File[] fileArray = file.listFiles();
                if (fileArray != null) {
                    for (File value : fileArray) {
                        scanDirRecursion(value);
                    }
                }
            } else {
                files.add(file);
            }
        }

        return files;
    }

}
